package com.shiyanlou.upms.system.controller;

import com.shiyanlou.upms.common.utils.R;
import com.shiyanlou.upms.system.domain.UpmsOrganization;
import com.shiyanlou.upms.system.service.UpmsOrganizationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 组织机构
 */
@Controller
@RequestMapping("/organization")
public class UpmsOrganizationController {

    @Autowired
    private UpmsOrganizationService upmsOrganizationService;

    @GetMapping("/main")
    public String UpmsOrganization(){
        return "organization/main";
    }


    @ResponseBody
    @GetMapping("/list")
    public List<UpmsOrganization> list(@RequestParam Map<String, Object> params){
        List<UpmsOrganization> upmsOrganizationList = upmsOrganizationService.list(params);
        return upmsOrganizationList;
    }

    @GetMapping("/add")
    String add(){
        return "organization/add";
    }

    @GetMapping("/edit/{organizationId}")
    String edit(@PathVariable("organizationId") Integer organizationId,Model model){
        UpmsOrganization upmsOrganization = upmsOrganizationService.selectByPrimaryKey(organizationId);
        model.addAttribute("upmsOrganization", upmsOrganization);
        return "organization/edit";
    }

    @ResponseBody
    @PostMapping("/save")
    public R save( UpmsOrganization upmsOrganization){
        if(upmsOrganizationService.insert(upmsOrganization)>0){
            return R.ok();
        }
        return R.error();
    }

    @PostMapping( "/remove")
    @ResponseBody
    public R remove(Integer organizationId){
        if(upmsOrganizationService.deleteByPrimaryKey(organizationId)>0){
            return R.ok();
        }
        return R.error();
    }

    @PostMapping( "/batchRemove")
    @ResponseBody
    public R remove(@RequestParam("ids[]") Integer[] organizationIds){
        upmsOrganizationService.batchRemove(organizationIds);
        return R.ok();
    }

    @RequestMapping("/update")
    @ResponseBody
    public R update(UpmsOrganization upmsOrganization){
        upmsOrganizationService.updateByPrimaryKeySelective(upmsOrganization);
        return R.ok();
    }

}
